#include <QMessageBox>
#include <QFile>

#include "pageerrortest.h"
#include "clickedLabel.h"

errorTestPage::errorTestPage(QWidget *parent)
    : QWidget(parent)
{
    setStyleSheet(".QLabel{min-height:20px;font-size:12px;}");
    setStyleSheet(".QPushButton{"
                      "font-size:17px;"
                      "font-weight:bold;"
                      "color: gold;"
                      "padding: 5px;"
                      "border-radius:5px;"
                      "background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #636363, stop:1 #292929);}"
                      "QPushButton:pressed{background-color:gray;}"
                  );

    QString sty="min-height:30px;min-width:120px;border: 1px solid #636363;border-radius: 5px;padding:2px;color:#444;background-color:rgb(130,160,192);font-size:16px;font-weight:bold;";
    QString sty2="min-height:30px;min-width:120px;border: 1px solid #636363;border-radius: 5px;padding:2px;color:#2e2;background-color:rgb(70,80,90);font-size:16px;font-weight:bold;";

    paintArea=new QWidget();
    paintArea->setStyleSheet("background-color:#000000;border:3px solid dark;");
    paintArea->setFixedSize(700,230);

    QLabel *labSyncN = new QLabel("同步状态");
    labSyncN->setFixedWidth(100);
    labSyncN->setStyleSheet("color:gray");
    labSync = new QLabel("未同步");
    labSync->setFixedSize(80,30);
    labSync->setStyleSheet("color:red;font-size:15px;font-weight:bold;border:1px solid gray;");
    labSync->setWordWrap(true);
    labSync->setAlignment(Qt::AlignVCenter);

    QLabel *labFreqN = new QLabel("电压频率");
    labFreqN->setStyleSheet("color:gray");
    labFreqN->setFixedWidth(100);
    labFreq = new QLabel("50 Hz");
    labFreq->setFixedSize(80,30);
    labFreq->setStyleSheet("color:green;font-size:15px;font-weight:bold;border:1px solid gray;");
    labFreq->setAlignment(Qt::AlignVCenter);

    QVBoxLayout *layoutLeftPaint = new QVBoxLayout;
    layoutLeftPaint->addWidget(labSyncN);
    layoutLeftPaint->addWidget(labSync);
    layoutLeftPaint->addSpacing(10);
    layoutLeftPaint->addWidget(labFreqN);
    layoutLeftPaint->addWidget(labFreq);
    layoutLeftPaint->addStretch(1);

    QLabel *labBlank = new QLabel("");
    labBlank->setStyleSheet("background-color:gray;");
    labBlank->setFixedSize(2,230);

    labUA1 = new ClickableLabel();
    labUA1->setText("UA");
    labUA1->setFixedSize(50,25);
    labUA1->setColor("#EEEE00");
    labUB1 = new ClickableLabel();
    labUB1->setText("UB");
    labUB1->setFixedSize(50,25);
    labUB1->setColor("green");
    labUC1 = new ClickableLabel();
    labUC1->setText("UC");
    labUC1->setFixedSize(50,25);
    labUC1->setColor("red");
    labIA1 = new ClickableLabel();
    labIA1->setText("IA");
    labIA1->setFixedSize(50,25);
    labIA1->setColor("#CD950C");
    labIB1 = new ClickableLabel();
    labIB1->setText("IB");
    labIB1->setFixedSize(50,25);
    labIB1->setColor("darkGreen");
    labIC1 = new ClickableLabel();
    labIC1->setText("IC");
    labIC1->setFixedSize(50,25);
    labIC1->setColor("darkRed");

    labUB1->setChecked(false);
    labUC1->setChecked(false);
    labIA1->setChecked(false);
    labIB1->setChecked(false);
    labIC1->setChecked(false);

    connect(labUA1, SIGNAL(Clicked(ClickableLabel*)), this, SLOT(changeChannelShow()));
    connect(labUB1, SIGNAL(Clicked(ClickableLabel*)), this, SLOT(changeChannelShow()));
    connect(labUC1, SIGNAL(Clicked(ClickableLabel*)), this, SLOT(changeChannelShow()));
    connect(labIA1, SIGNAL(Clicked(ClickableLabel*)), this, SLOT(changeChannelShow()));
    connect(labIB1, SIGNAL(Clicked(ClickableLabel*)), this, SLOT(changeChannelShow()));
    connect(labIC1, SIGNAL(Clicked(ClickableLabel*)), this, SLOT(changeChannelShow()));

    QVBoxLayout *layoutShowControl = new QVBoxLayout;
    layoutShowControl->setContentsMargins(0,0,0,0);
    layoutShowControl->setSpacing(5);
    layoutShowControl->addStretch(1);
    layoutShowControl->addWidget(labUA1);
    layoutShowControl->addWidget(labUB1);
    layoutShowControl->addWidget(labUC1);
    layoutShowControl->addWidget(labIA1);
    layoutShowControl->addWidget(labIB1);
    layoutShowControl->addWidget(labIC1);
    layoutShowControl->addStretch(1);

    QHBoxLayout *layoutPaintArea = new QHBoxLayout;
    layoutPaintArea->setContentsMargins(20,10,20,10);
    layoutPaintArea->addLayout(layoutLeftPaint);
    layoutPaintArea->addWidget(labBlank);
    layoutPaintArea->addWidget(paintArea);
    layoutPaintArea->addLayout(layoutShowControl);
    layoutPaintArea->addStretch(1);

    QWidget *part3 = new QWidget(this);
    part3->setStyleSheet(".QWidget{border:2px solid #444;}");
    part3->setFixedHeight(250);
    part3->setLayout(layoutPaintArea);

    resultAnalyze = new staticPage(this);
    harmonicAnalyze = new pageHarmonic(this);
    powerAnalyze = new powerPage(this);

    QScrollArea *scrollArea = new QScrollArea(this);
    scrollArea->setStyleSheet(".QScrollBar{"
                              "background:#bbb; width: 30px;"
                              "}");
    scrollArea->setFixedSize(1000,300);
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(harmonicAnalyze);

    extPages = new QTabWidget(this);
    extPages->setFixedSize(1000,350);
    extPages->addTab(part3, "查看波形");
    extPages->addTab(resultAnalyze, "误差统计");
    extPages->addTab(scrollArea, "谐波分析");
    extPages->addTab(powerAnalyze, "功率分析");

    //----------------------------------------------
    QLabel *labRealValue = new QLabel(tr("标准有效值(%)"));
    labRealValue->setFixedWidth(100);
    labRealValue->setStyleSheet("color:gray;");
    QLabel *labTestValue = new QLabel(tr("被测有效值(%)"));
    labTestValue->setFixedWidth(100);
    labTestValue->setStyleSheet("color:gray;");
    QLabel *labRealPhase = new QLabel(tr("标准相角"));
    labRealPhase->setFixedWidth(100);
    labRealPhase->setStyleSheet("color:gray;");
    QLabel *labTestPhase = new QLabel(tr("被测相角"));
    labTestPhase->setFixedWidth(100);
    labTestPhase->setStyleSheet("color:gray;");

    QLabel *labError = new QLabel(tr("比差(%)"));
    labError->setFixedWidth(100);
    labError->setStyleSheet("color:lightgray;");
    QLabel *labDelay = new QLabel(("角差(')"));
    labDelay->setFixedWidth(100);
    labDelay->setStyleSheet("color:lightgray;");

    QLabel *labPhaseAU = new QLabel("Ua");
    labPhaseAU->setStyleSheet("color:#EEEE00;font-size:20px;font-weight:bold;");
    QLabel *labPhaseAI = new QLabel("Ia");
    labPhaseAI->setStyleSheet("color:#CD950C;font-size:20px;font-weight:bold;");

    QLabel *labPhaseBU = new QLabel("Ub");
    labPhaseBU->setStyleSheet("color:green;font-size:20px;font-weight:bold;");
    QLabel* labPhaseBI = new QLabel("Ib");
    labPhaseBI->setStyleSheet("color:darkGreen;font-size:20px;font-weight:bold;");

    QLabel* labPhaseCU = new QLabel("Uc");
    labPhaseCU->setStyleSheet("color:red;font-size:20px;font-weight:bold;");
    QLabel* labPhaseCI = new QLabel("Ic");
    labPhaseCI->setStyleSheet("color:darkRed;font-size:20px;font-weight:bold;");

    labPhaseAURms = new QLabel(tr("0.00000"));
    labPhaseAURms->setStyleSheet(sty);
    labPhaseAURms->setFixedWidth(100);
    labPhaseAIRms = new QLabel(tr("0.00000"));
    labPhaseAIRms->setStyleSheet(sty);
    labPhaseAIRms->setFixedWidth(100);

    labPhaseBURms = new QLabel(tr("0.00000"));
    labPhaseBURms->setStyleSheet(sty);
    labPhaseBURms->setFixedWidth(100);
    labPhaseBIRms = new QLabel(tr("0.00000"));
    labPhaseBIRms->setStyleSheet(sty);
    labPhaseBIRms->setFixedWidth(100);

    labPhaseCURms = new QLabel(tr("0.00000"));
    labPhaseCURms->setStyleSheet(sty);
    labPhaseCURms->setFixedWidth(100);
    labPhaseCIRms = new QLabel(tr("0.00000"));
    labPhaseCIRms->setStyleSheet(sty);
    labPhaseCIRms->setFixedWidth(100);

    // real value of the standard
    labPhaseAUReal = new QLabel(tr("0.00000"));
    labPhaseAUReal->setStyleSheet(sty);
    labPhaseAUReal->setFixedWidth(100);
    labPhaseAIReal = new QLabel(tr("0.00000"));
    labPhaseAIReal->setStyleSheet(sty);
    labPhaseAIReal->setFixedWidth(100);

    labPhaseBUReal = new QLabel(tr("0.00000"));
    labPhaseBUReal->setStyleSheet(sty);
    labPhaseBUReal->setFixedWidth(100);
    labPhaseBIReal = new QLabel(tr("0.00000"));
    labPhaseBIReal->setStyleSheet(sty);
    labPhaseBIReal->setFixedWidth(100);

    labPhaseCUReal = new QLabel(tr("0.00000"));
    labPhaseCUReal->setStyleSheet(sty);
    labPhaseCUReal->setFixedWidth(100);
    labPhaseCIReal = new QLabel(tr("0.00000"));
    labPhaseCIReal->setStyleSheet(sty);
    labPhaseCIReal->setFixedWidth(100);

    //test value of the standard
    labPhaseAUTest = new QLabel(tr("0.00000"));
    labPhaseAUTest->setStyleSheet(sty);
    labPhaseAUTest->setFixedWidth(100);
    labPhaseAITest = new QLabel(tr("0.00000"));
    labPhaseAITest->setStyleSheet(sty);
    labPhaseAITest->setFixedWidth(100);

    labPhaseBUTest = new QLabel(tr("0.00000"));
    labPhaseBUTest->setStyleSheet(sty);
    labPhaseBUTest->setFixedWidth(100);
    labPhaseBITest = new QLabel(tr("0.00000"));
    labPhaseBITest->setStyleSheet(sty);
    labPhaseBITest->setFixedWidth(100);

    labPhaseCUTest = new QLabel(tr("0.00000"));
    labPhaseCUTest->setStyleSheet(sty);
    labPhaseCUTest->setFixedWidth(100);
    labPhaseCITest = new QLabel(tr("0.00000"));
    labPhaseCITest->setStyleSheet(sty);
    labPhaseCITest->setFixedWidth(100);

    // error of the test value
    labPhaseAUError = new QLabel(tr("0.00000"));
    labPhaseAUError->setStyleSheet(sty2);
    labPhaseAUError->setFixedWidth(100);
    labPhaseAIError = new QLabel(tr("0.00000"));
    labPhaseAIError->setStyleSheet(sty2);
    labPhaseAIError->setFixedWidth(100);
    labPhaseBUError = new QLabel(tr("0.00000"));
    labPhaseBUError->setStyleSheet(sty2);
    labPhaseBUError->setFixedWidth(100);
    labPhaseBIError = new QLabel(tr("0.00000"));
    labPhaseBIError->setStyleSheet(sty2);
    labPhaseBIError->setFixedWidth(100);

    labPhaseCUError = new QLabel(tr("0.00000"));
    labPhaseCUError->setStyleSheet(sty2);
    labPhaseCUError->setFixedWidth(100);
    labPhaseCIError = new QLabel(tr("0.00000"));
    labPhaseCIError->setStyleSheet(sty2);
    labPhaseCIError->setFixedWidth(100);

    //real phase of the standard
    labAUPhaseReal = new QLabel(tr("0.00000"));
    labAUPhaseReal->setStyleSheet(sty);
    labAUPhaseReal->setFixedWidth(100);
    labBUPhaseReal = new QLabel(tr("0.00000"));
    labBUPhaseReal->setStyleSheet(sty);
    labBUPhaseReal->setFixedWidth(100);
    labCUPhaseReal = new QLabel(tr("0.00000"));
    labCUPhaseReal->setStyleSheet(sty);
    labCUPhaseReal->setFixedWidth(100);

    labAIPhaseReal = new QLabel(tr("0.00000"));
    labAIPhaseReal->setStyleSheet(sty);
    labAIPhaseReal->setFixedWidth(100);
    labBIPhaseReal = new QLabel(tr("0.00000"));
    labBIPhaseReal->setStyleSheet(sty);
    labBIPhaseReal->setFixedWidth(100);
    labCIPhaseReal = new QLabel(tr("0.00000"));
    labCIPhaseReal->setStyleSheet(sty);
    labCIPhaseReal->setFixedWidth(100);

    //phase from 9-2
    labAUPhaseTest = new QLabel(tr("0.00000"));
    labAUPhaseTest->setStyleSheet(sty);
    labAUPhaseTest->setFixedWidth(100);
    labAIPhaseTest = new QLabel(tr("0.00000"));
    labAIPhaseTest->setStyleSheet(sty);
    labAIPhaseTest->setFixedWidth(100);

    labBUPhaseTest = new QLabel(tr("0.00000"));
    labBUPhaseTest->setStyleSheet(sty);
    labBUPhaseTest->setFixedWidth(100);
    labBIPhaseTest = new QLabel(tr("0.00000"));
    labBIPhaseTest->setStyleSheet(sty);
    labBIPhaseTest->setFixedWidth(100);

    labCUPhaseTest = new QLabel(tr("0.00000"));
    labCUPhaseTest->setStyleSheet(sty);
    labCUPhaseTest->setFixedWidth(100);
    labCIPhaseTest = new QLabel(tr("0.00000"));
    labCIPhaseTest->setStyleSheet(sty);
    labCIPhaseTest->setFixedWidth(100);

    // pahse error the test
    labPhaseAUDelay = new QLabel(tr("0.00000"));
    labPhaseAUDelay->setStyleSheet(sty2);
    labPhaseAUDelay->setFixedWidth(100);
    labPhaseBUDelay = new QLabel(tr("0.00000"));
    labPhaseBUDelay->setStyleSheet(sty2);

    labPhaseBUDelay->setFixedWidth(100);
    labPhaseCUDelay = new QLabel(tr("0.00000"));
    labPhaseCUDelay->setStyleSheet(sty2);

    labPhaseCUDelay->setFixedWidth(100);

    labPhaseAIDelay = new QLabel(tr("0.00000"));
    labPhaseAIDelay->setStyleSheet(sty2);

    labPhaseAIDelay->setFixedWidth(100);
    labPhaseBIDelay = new QLabel(tr("0.00000"));
    labPhaseBIDelay->setStyleSheet(sty2);

    labPhaseBIDelay->setFixedWidth(100);
    labPhaseCIDelay = new QLabel(tr("0.00000"));
    labPhaseCIDelay->setStyleSheet(sty2);

    labPhaseCIDelay->setFixedWidth(100);

    QGridLayout *layoutResult = new QGridLayout;

    layoutResult->addWidget(labPhaseAU,0,1,1,1,Qt::AlignHCenter);
    layoutResult->addWidget(labPhaseAI,0,4,1,1,Qt::AlignHCenter);
    layoutResult->addWidget(labPhaseBU,0,2,1,1,Qt::AlignHCenter);
    layoutResult->addWidget(labPhaseBI,0,5,1,1,Qt::AlignHCenter);
    layoutResult->addWidget(labPhaseCU,0,3,1,1,Qt::AlignHCenter);
    layoutResult->addWidget(labPhaseCI,0,6,1,1,Qt::AlignHCenter);

//    layoutResult->addWidget(labRealRms,1,0,1,1);
    layoutResult->addWidget(labRealValue,2,0,1,1);
    layoutResult->addWidget(labTestValue,3,0,1,1);
    layoutResult->addWidget(labError,4,0,1,1);
    layoutResult->addWidget(labRealPhase,5,0,1,1);
    layoutResult->addWidget(labTestPhase,7,0,1,1);
    layoutResult->addWidget(labDelay,8,0,1,1);

//    layoutResult->addWidget(labPhaseAURms,1,1,1,1);
//    layoutResult->addWidget(labPhaseAIRms,1,2,1,1);
//    layoutResult->addWidget(labPhaseBURms,1,3,1,1);
//    layoutResult->addWidget(labPhaseBIRms,1,4,1,1);
//    layoutResult->addWidget(labPhaseCURms,1,5,1,1);
//    layoutResult->addWidget(labPhaseCIRms,1,6,1,1);

    layoutResult->addWidget(labPhaseAUReal,2,1,1,1);
    layoutResult->addWidget(labPhaseBUReal,2,2,1,1);
    layoutResult->addWidget(labPhaseCUReal,2,3,1,1);

    layoutResult->addWidget(labPhaseAIReal,2,4,1,1);
    layoutResult->addWidget(labPhaseBIReal,2,5,1,1);
    layoutResult->addWidget(labPhaseCIReal,2,6,1,1);

    layoutResult->addWidget(labPhaseAUTest,3,1,1,1);
    layoutResult->addWidget(labPhaseBUTest,3,2,1,1);
    layoutResult->addWidget(labPhaseCUTest,3,3,1,1);

    layoutResult->addWidget(labPhaseAITest,3,4,1,1);
    layoutResult->addWidget(labPhaseBITest,3,5,1,1);
    layoutResult->addWidget(labPhaseCITest,3,6,1,1);

    layoutResult->addWidget(labPhaseAUError,4,1,1,1);
    layoutResult->addWidget(labPhaseBUError,4,2,1,1);
    layoutResult->addWidget(labPhaseCUError,4,3,1,1);

    layoutResult->addWidget(labPhaseAIError,4,4,1,1);
    layoutResult->addWidget(labPhaseBIError,4,5,1,1);
    layoutResult->addWidget(labPhaseCIError,4,6,1,1);

    layoutResult->addWidget(labAUPhaseReal,5,1,1,1);
    layoutResult->addWidget(labBUPhaseReal,5,2,1,1);
    layoutResult->addWidget(labCUPhaseReal,5,3,1,1);

    layoutResult->addWidget(labAIPhaseReal,5,4,1,1);
    layoutResult->addWidget(labBIPhaseReal,5,5,1,1);
    layoutResult->addWidget(labCIPhaseReal,5,6,1,1);

    layoutResult->addWidget(labAUPhaseTest,7,1,1,1);
    layoutResult->addWidget(labBUPhaseTest,7,2,1,1);
    layoutResult->addWidget(labCUPhaseTest,7,3,1,1);

    layoutResult->addWidget(labAIPhaseTest,7,4,1,1);
    layoutResult->addWidget(labBIPhaseTest,7,5,1,1);
    layoutResult->addWidget(labCIPhaseTest,7,6,1,1);

    layoutResult->addWidget(labPhaseAUDelay,8,1,1,1);
    layoutResult->addWidget(labPhaseBUDelay,8,2,1,1);
    layoutResult->addWidget(labPhaseCUDelay,8,3,1,1);

    layoutResult->addWidget(labPhaseAIDelay,8,4,1,1);
    layoutResult->addWidget(labPhaseBIDelay,8,5,1,1);
    layoutResult->addWidget(labPhaseCIDelay,8,6,1,1);

    QWidget *part1 = new QWidget;
    part1->setLayout(layoutResult);
    part1->setStyleSheet(".QWidget{ border:2px solid #444;}");

    sourcePage = new pageSource;

    pageAll = new pagePacketDetail;
    pageAll->setObjectName("pageAll");

    mainPages = new QStackedWidget;
    mainPages->addWidget(part1);
    mainPages->addWidget(sourcePage);
    mainPages->addWidget(pageAll);

    btnStart=new QPushButton(tr("开始"));
    btnStart->setStyleSheet("font-size:17px;color:gold;");
    btnStart->setFixedSize(100,60);

    btnBack=new QPushButton(tr("完成"));
    btnBack->setStyleSheet("font-size:17px;");
    btnBack->setFixedSize(100,60);
    connect(btnBack, SIGNAL(clicked(bool)), this, SLOT(back2Main()));
    btnBack->hide();

    btnSource = new QPushButton("功率源设置");
    btnSource->setStyleSheet("font-size:15px;color:lightgray;");
    btnSource->setFixedSize(100,40);
    connect(btnSource, SIGNAL(clicked(bool)), this, SLOT(chageMainPage()));

    btnAnalyzeProtocol=new QPushButton(tr("全帧解析"));
    btnAnalyzeProtocol->setStyleSheet("font-size:15px;color:lightgray;");
    btnAnalyzeProtocol->setFixedSize(100,40);
    connect(btnAnalyzeProtocol, SIGNAL(clicked(bool)), this, SLOT(chageMainPage2()));

    QHBoxLayout *controlLayout = new QHBoxLayout;
    controlLayout->setContentsMargins(20,0,30,0);
    controlLayout->addWidget(btnSource);
    controlLayout->addStretch(1);
    controlLayout->addWidget(btnAnalyzeProtocol);
    controlLayout->addSpacing(5);
    controlLayout->addWidget(btnStart);
    controlLayout->addWidget(btnBack);

    QVBoxLayout *layoutMain = new QVBoxLayout;
    layoutMain->addSpacing(20);
    layoutMain->addWidget(mainPages);
    layoutMain->addLayout(controlLayout);
    layoutMain->addStretch(1);
    layoutMain->addWidget(extPages);

    setLayout(layoutMain);

    memset(digi_tdlb_show, false, sizeof(digi_tdlb_show));
    digi_tdlb_show[0] = true;
}

void errorTestPage::chageMainPage()
{
     mainPages->setCurrentIndex(1);

     btnSource->hide();
     btnStart->hide();
     btnAnalyzeProtocol->hide();

     btnBack->show();
     btnBack->setText("完成");
}

void errorTestPage::chageMainPage2()
{
    mainPages->setCurrentIndex(2);

    btnSource->hide();
    btnStart->hide();
    btnAnalyzeProtocol->hide();

    btnBack->show();
    btnBack->setText("返回");
}

void errorTestPage::back2Main()
{
    mainPages->setCurrentIndex(0);

    btnSource->show();
    btnAnalyzeProtocol->show();
    btnStart->show();

    btnBack->hide();
}

void errorTestPage::changeChannelShow()
{
    memset(digi_tdlb_show, false, sizeof(digi_tdlb_show));

    ClickableLabel *clicked = (ClickableLabel *)sender();

    if(!clicked->isChecked())
    {
        clicked->setChecked(true);
        return;
    }

    labUA1->setChecked(false);
    labUB1->setChecked(false);
    labUC1->setChecked(false);
    labIA1->setChecked(false);
    labIB1->setChecked(false);
    labIC1->setChecked(false);

    clicked->setChecked(true);

    if(labUA1->isChecked())
    {
        digi_tdlb_show[0] = true;
    }

    if(labUB1->isChecked())
    {
        digi_tdlb_show[1] = true;
    }

    if(labUC1->isChecked())
    {
        digi_tdlb_show[2] = true;
    }

    if(labIA1->isChecked())
    {
        digi_tdlb_show[3] = true;
    }

    if(labIB1->isChecked())
    {
        digi_tdlb_show[4] = true;
    }

    if(labIC1->isChecked())
    {
        digi_tdlb_show[5] = true;
    }

    qDebug()<<"changeChannelShow()"<<digi_tdlb_show[0]<<digi_tdlb_show[1]<<digi_tdlb_show[2]<<digi_tdlb_show[3]<<digi_tdlb_show[4]<<digi_tdlb_show[5];
}
